package it.trick.admin.service;

import com.google.gson.Gson;
import it.trick.admin.db.AccountMstEntity;
import it.trick.admin.db.DBManager;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;

public class Http {

    public static String get(String urlString) throws Exception {
        BufferedReader reader = null;
        try {
            URL url = new URL(urlString);
            reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
            StringBuilder buffer = new StringBuilder();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1) {
                buffer.append(chars, 0, read);
            }

            return buffer.toString();
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

    public static Object postGetResponseJson(URL postURL, String postString, Type responseModelType) throws Exception {
        HttpURLConnection http = (HttpURLConnection)postURL.openConnection();
        http.setRequestMethod("POST");
        
        AccountMstEntity mstEntity = DBManager.getAccountInfo(DBManager.getConn(), "it.trick.cos@gmail.com");
        http.setRequestProperty("Authorization", " OAuth " + mstEntity.accessToken);
        System.out.println("Authorization:OAuth " + mstEntity.accessToken);

        // 送信する指定
        http.setDoOutput(true);

        // ---------------------------------------------------------------------
        // 送信処理
        // ---------------------------------------------------------------------
        OutputStreamWriter ow = new OutputStreamWriter(http.getOutputStream(), "utf-8");
        BufferedWriter bw = new BufferedWriter(ow);

        //POSTの内容を書き出す
        bw.write(postString);
        bw.flush();

        // クローズ
        bw.close();
        ow.close();

        // ---------------------------------------------------------------------
        // 結果受け取り
        // ---------------------------------------------------------------------
        InputStreamReader ir = new InputStreamReader(http.getInputStream(), "utf-8");
        BufferedReader br1 = new BufferedReader(ir);
        StringBuilder rsb = new StringBuilder();

        int read;
        char[] chars = new char[1024];
        while ((read = br1.read(chars)) != -1) {
            rsb.append(chars, 0, read);
        }

        // ---------------------------------------------------------------------
        // JSONデータの生成
        // ---------------------------------------------------------------------
        String json = rsb.toString();
        Gson gson = new Gson();

        // クローズ
        br1.close();
        ir.close();
        
        System.out.println(json);

        return gson.fromJson(json, responseModelType);
    }

    public static String postOnle(URL postURL, String postString) throws Exception {
        HttpURLConnection http = (HttpURLConnection)postURL.openConnection();
        http.setRequestMethod("POST");

        // 送信する指定
        http.setDoOutput(true);

        // ---------------------------------------------------------------------
        // 送信処理
        // ---------------------------------------------------------------------
        OutputStreamWriter ow = new OutputStreamWriter(http.getOutputStream(), "utf-8");
        BufferedWriter bw = new BufferedWriter(ow);

        //POSTの内容を書き出す
        bw.write(postString);
        bw.flush();

        // クローズ
        bw.close();
        ow.close();


        // ---------------------------------------------------------------------
        // 結果受け取り
        // ---------------------------------------------------------------------
        InputStreamReader ir = new InputStreamReader(http.getInputStream(), "utf-8");
        BufferedReader br1 = new BufferedReader(ir);
        StringBuilder rsb = new StringBuilder();

        String line;
        while ((line = br1.readLine()) != null) {
            rsb.append(line);
        }

        // クローズ
        br1.close();
        ir.close();

        return rsb.toString();
    }
}
